package com.fansl.allround.message.listenner;

import com.alibaba.fastjson.JSONObject;
import com.corundumstudio.socketio.AckRequest;
import com.corundumstudio.socketio.SocketIOClient;
import com.corundumstudio.socketio.listener.DataListener;
import com.fansl.allround.message.api.message.ChatMessage;
import com.fansl.allround.message.handler.ChatEventHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author fansl
 * @Description: 聊天事件监听器
 * @date 2019/9/4 17:09
 */
@Slf4j
@Component
public class ChatEventListener implements DataListener<ChatMessage> {
    @Resource
    private ChatEventHandler chatEventHandler;

    @Override
    public void onData(SocketIOClient client, ChatMessage chatMessage, AckRequest ackRequest) throws Exception {
        log.info(client.getRemoteAddress()+" web客户端发来消息:{}",JSONObject.toJSONString(chatMessage));
        switch (chatMessage.getChatType()){
            case GROUP:
                chatEventHandler.handleGroupMessage(chatMessage);
                break;
            case GROUP_TO_SINGLE:
                chatEventHandler.handleGroupToSingleMessage(chatMessage);
                break;
            case SINGLE:
                chatEventHandler.handleSingleMessage(chatMessage);
                break;
            default:
                break;
        }
        if(ackRequest.isAckRequested()){
            log.info("client message was delivered to server!");
            ackRequest.sendAckData("client message was delivered to server!","yeah!");
        }
    }
}
